package com.ttb.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class MessageDAO {
	private SQLiteDatabase database;
	private DBHandler dbHandler;
	private String[] allColums = {DBHandler.SMS_ID,
			DBHandler.SMS_ADDRESS,
			DBHandler.SMS_PERSON,
			DBHandler.SMS_DATE,
			DBHandler.SMS_READ,
			DBHandler.SMS_STATUS,
			DBHandler.SMS_TYPE,
			DBHandler.SMS_BODY,
			DBHandler.SMS_SEEN
			};
	public static final String SMS_TABLE = DBHandler.SMS_TABLE;
	public static final String ID = DBHandler.SMS_ID ;
	public static final String ADRESS = DBHandler.SMS_ADDRESS ;
    public static final String PERSON = DBHandler.SMS_PERSON;
    public static final String DATE = DBHandler.SMS_DATE;
    public static final String READ = DBHandler.SMS_READ;
    public static final String STATUS = DBHandler.SMS_STATUS;
    public static final String TYPE = DBHandler.SMS_TYPE ;
    public static final String BODY = DBHandler.SMS_BODY;
    public static final String SEEN = DBHandler.SMS_SEEN;
	
	public MessageDAO(Context context) {
		dbHandler = new DBHandler(context);
	}
	public void open()
	{
		database = dbHandler.getWritableDatabase();
	}
	public void close()
	{
		dbHandler.close();
	}
	//add message
	public void addMessage(MessageSMS sms)
	{
		this.open();
		ContentValues values = new ContentValues();
		values.put(ADRESS, sms.getAddress());
		values.put(PERSON, sms.getPerson());
		values.put(DATE, sms.getDate());
		values.put(READ, sms.getRead());
		values.put(STATUS, sms.getStatus());
		values.put(TYPE, sms.getType());
		values.put(BODY, sms.getBody());
		values.put(SEEN, sms.getSeen());
		//insert row
		database.insert(SMS_TABLE, null, values);
		this.close();
	}
	//get single message
	public MessageSMS  getMessage(int id) {
		Cursor cursor = database.query(SMS_TABLE, 
				allColums, 
				ID + "=?", new String[] {String.valueOf(id)}, 
				null, null, null);
		if(cursor != null)
		{
			cursor.moveToFirst();
			
		}
		MessageSMS sms = cursorToMessage(cursor);
		cursor.close();
		//return message
		return sms;
	}
	
	//Get all messages
	public List<MessageSMS> getAllMessages() {
		List<MessageSMS> smsList = new ArrayList<MessageSMS>();
	    // Select All Query
	    String selectQuery = "SELECT  * FROM " + SMS_TABLE;
	 
	    this.open();
	    Cursor cursor = database.rawQuery(selectQuery, null);
	 
	    // looping through all rows and adding to list
	    if (cursor.moveToFirst()) {
	        do {
	            MessageSMS sms = cursorToMessage(cursor);
	            // Adding sms to list
	            smsList.add(sms);
	        } while (cursor.moveToNext());
	    }
	    cursor.close();
	    this.close();
	    // return message list
	    return smsList;
	}
	//delete single message
	public void deleteMessage(int id)
	{
		this.open();
		Log.d("DELETE SMS ID",String.valueOf(id) );
		database.delete(SMS_TABLE, ID + "=?" , new String[]{String.valueOf(id)});
		this.close();
	}
	//update single message
	public void updateMessage(MessageSMS sms)
	{
		this.open();
		ContentValues values = new ContentValues();
		values.put(ADRESS, sms.getAddress());
		values.put(PERSON, sms.getPerson());
		values.put(DATE, sms.getDate());
		values.put(READ, sms.getRead());
		values.put(STATUS, sms.getStatus());
		values.put(TYPE, sms.getType());
		values.put(BODY, sms.getBody());
		values.put(SEEN, sms.getSeen());
		//update
		database.update(SMS_TABLE, values,ID + "=?",
				new String[]{String.valueOf(sms.getId())});
		this.close();
	}
	// Getting messages Count
    public int getMessageCount() {
    	
        String countQuery = "SELECT  * FROM " + SMS_TABLE;
        this.open();
        Cursor cursor = database.rawQuery(countQuery, null);
        cursor.close();
        this.close();
        // return count
        return cursor.getCount();
    }
    private MessageSMS cursorToMessage(Cursor cursor)
    {
    	MessageSMS sms = new MessageSMS();
		sms.setId(cursor.getInt(0));
		sms.setAddress(cursor.getString(1));
		sms.setPerson(cursor.getString(2));
		sms.setDate(cursor.getInt(3));
		sms.setRead(cursor.getInt(4));
		sms.setStatus(cursor.getInt(5));
		sms.setType(cursor.getInt(6));
		sms.setBody(cursor.getString(7));
		sms.setSeen(cursor.getInt(8));
		return sms;
    }
  //Get all messages
  	public List<MessageSMS> getAllMessagesFromPhoneNumber(String phoneNumber) {
  		List<MessageSMS> smsList = new ArrayList<MessageSMS>();
  	    // Select All Query
		this.open();
  	  Cursor cursor = database.query(SMS_TABLE, 
				allColums, 
				ADRESS + "=?", new String[] {phoneNumber}, 
				null, null, null);
  	    // looping through all rows and adding to list
  	  	if(cursor != null && cursor.getCount() > 0 )
  	  	{
	  	    if (cursor.moveToFirst()) {
	  	        do {
	  	            MessageSMS sms = cursorToMessage(cursor);
	  	            // Adding sms to list
	  	            smsList.add(sms);
	  	        } while (cursor.moveToNext());
	  	    }
  	  	}
  	    cursor.close();
  	    this.close();
  	    // return message list
  	    return smsList;
  	}
}
